<?php


namespace seecms\region\lib;

use seecms\lib\builder\control\Control;
use seecms\SeeLang;

class RegionControl extends Control
{
    protected $type = 'region';

    /**
     * 省份字段
     * @var string
     */
    protected $province_field = 'province_code';

    /**
     * 城市字段
     * @var string
     */
    protected $city_field = 'city_code';

    /**
     * 县区字段
     * @var string
     */
    protected $area_field = 'area_code';

    protected $level = 3;

    /**
     * @return string
     */
    public function getProvinceField(): string
    {
        return $this->province_field;
    }

    /**
     * @param string $province_field
     */
    public function setProvinceField(string $province_field)
    {
        $this->province_field = $province_field;
    }

    /**
     * @return string
     */
    public function getCityField(): string
    {
        return $this->city_field;
    }

    /**
     * @param string $city_field
     */
    public function setCityField(string $city_field)
    {
        $this->city_field = $city_field;
    }

    /**
     * @return string
     */
    public function getAreaField(): string
    {
        return $this->area_field;
    }

    /**
     * @param string $area_field
     */
    public function setAreaField(string $area_field)
    {
        $this->area_field = $area_field;
    }

    /**
     * @return int
     */
    public function getSize(): int
    {
        $level = $this->getLevel();
        $size = 2;
        if ($level == 2) {
            $size = 4;
        } elseif ($level == 3) {
            $size = 6;
        }

        return $size;
    }

    /**
     * @return int
     */
    public function getLevel(): int
    {
        return $this->level;
    }

    /**
     * @param int $level
     * @return $this
     */
    public function setLevel(int $level): RegionQuery
    {
        if ($level > 3 || $level <= 0) {
            $level = 3;
        }

        $this->level = $level;

        return $this;
    }

    public function render(): string
    {
        $required = $this->required ? '<span class="layui-font-red">*</span>' : '';
        $readonly = $this->readonly ? 'readonly' : '';
        $disabled = $this->disabled ? 'disabled' : '';

        $display = 'block';
        $container = '<div class="layui-form-item" data-region-container>';
        if ($this->inline == 'inline') {
            $display = 'inline';
            $container = '<div class="layui-inline" data-region-container>';
        }

        $label = $this->label ?: SeeLang::instance()->get('region.region');

        $province_holder = SeeLang::instance()->get('region.province_holder');
        $city_holder = SeeLang::instance()->get('region.city_holder');
        $area_holder = SeeLang::instance()->get('region.area_holder');

        $province_field = $this->getProvinceField();
        $city_field = $this->getCityField();
        $area_field = $this->getAreaField();

        $province = <<<EOF
    <div class="layui-input-$display">
        <select name="{$province_field}" lay-filter="{$province_field}"
            lay-search
            $readonly
            $disabled
        >
            <option value="">{$province_holder}</option>
        </select>
    </div>
EOF;

        $city = <<<EOF
    <div class="layui-input-$display">
        <select name="{$city_field}" lay-filter="{$city_field}"
            lay-search
            $readonly
            $disabled
        >
            <option value="">{$city_holder}</option>
        </select>
    </div>
EOF;


        $area = <<<EOF
    <div class="layui-input-$display">
        <select name="{$area_field}" lay-filter="{$area_field}"
            lay-search
            $readonly
            $disabled
        >
            <option value="">{$area_holder}</option>
        </select>
    </div>
EOF;
        
        $select = $province;

        if ($this->getLevel() == 2) {
            $select .= $city;
        } elseif ($this->getLevel() == 3) {
            $select .= $city . $area;
        }

        return <<<EOF
$container
   <label class="layui-form-label">
        $label
        {$required}
    </label>
    
    $select
   
</div>
EOF;
    }
}